package com.bblocks.standard.controller;


import cn.hutool.core.util.StrUtil;
import com.bblocks.area.AreaVo;
import com.bblocks.common.BaseController;
import com.bblocks.common.data.handle.IAreaCrawlingHandle;
import com.bblocks.company.CompanyExtraVo;
import com.bblocks.util.R;
import com.bblocks.util.StringUtil;
import com.google.common.collect.Lists;
import com.pig4cloud.pig.admin.api.entity.SysArea;
import com.pig4cloud.pig.admin.api.feign.RemoteAreaService;
import com.pig4cloud.pig.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 系统配置(SysParam)表控制层
 *
 * @author makejava
 * @since 2021-10-29 13:57:44
 */
@Slf4j
@Tag(description = "area", name = "area对象")
@RestController
@RequestMapping("/area")
public class AreaController extends BaseController {

	@Resource
	IAreaCrawlingHandle iAreaCrawlingHandle;
	@Resource
	RemoteAreaService remoteAreaService;

	@Inner(false)
	@Operation(summary = "爬取(0-4)", description = "")
	@PostMapping("/crawlingAll{level}")
    public R<List<AreaVo>> crawlingAll(@PathVariable Integer level) {
		R<List<AreaVo>> ret = iAreaCrawlingHandle.crawlingAll(level);
        return ret;
    }

	@Inner(false)
	@Operation(summary = "爬取指定地址(0-4)", description = ""
			,parameters = {
			@Parameter(name = "level", description = "等级（0-4）",in = ParameterIn.PATH),
			@Parameter(name = "url", description = "指定地址(为空则全部)",required = false),
		}
	)
	@PostMapping("/crawling/{level}")
	public R<List<AreaVo>> crawling(@PathVariable Integer level,String url) {
		if(StrUtil.isEmpty(url)){
			R<List<AreaVo>> ret = iAreaCrawlingHandle.crawlingAll(level);
			return ret;
		}else{
			R<List<AreaVo>> ret = iAreaCrawlingHandle.crawlingArea(url,level);
			return ret;
		}
	}

	@Inner(false)
	@GetMapping("/detailsByCode/{code}")
	public R<SysArea> getByCode(@PathVariable String code) {
		R<SysArea> area =	remoteAreaService.getByCode(code);
		return area;
	}

	@Inner(false)
	@Operation(summary = "获取下级数据", description = ""
			,parameters = {
			@Parameter(name = "parentCode", description = "父code(为空则省份)",required = false),
		}
	)
	@GetMapping("/getByParentCode")
	public R<List<SysArea>> getByParentCode(String parentCode) {
		R<List<SysArea>> areas = remoteAreaService.getByParentCode(parentCode);
		return areas;
	}

	@Inner(false)
	@Operation(summary = "重新刷新区域数据", description = ""
	)
	@PostMapping ("/freshAreas")
	public R freshAreas() {
		R ret = remoteAreaService.initAll();
		log.info("刷新结果：{}-{}",ret.getCode(),ret.getMsg());
		return R.ok();
	}

}

